"""
    Author:
    Date: 2019-12-12
"""

from . import usecases
from entities import sms_mgr
from entities import user_mgr
from entities.err import *
from utils import ajax
from utils.log import Log
from utils import haibao_util

from . helpers import mobile_token_required
from . helpers import uid_token_required

def _json(data):
    """ 将ApiServer返回的数据构造成给客户端展现的格式 """
    code, msg, info = data
    if code != 0:
        return ajax.error_response(msg, code)
    return ajax.success_response(info)


# -----------------------------------------------------------
# Part of MpPay
# -----------------------------------------------------------

def api_user_info(request):
    """ 查询用户基本信息 """
    uid = request.POST['uid'].strip()

    err, data = usecases.api_user_info(request, uid)
    if err != ERROR_NO_ERROR:
        return ajax.error_response(err['message'], err['id'])

    return ajax.success_response(data)

def api_get_hsq_mp_prepay_info(request):
    """ 获取慧收钱的公众号预支付信息 """
    uid = int(request.POST['uid'].strip())
    amount = int(request.POST['amount'].strip())

    err, data = usecases.api_get_hsq_mp_prepay_info(request, uid, amount)
    if err != ERROR_NO_ERROR:
        return ajax.error_response(err['message'], err['id'])

    return ajax.success_response(data)

# -----------------------------------------------------------
# Part of Withdraw
# -----------------------------------------------------------

def api_fetch_smscode(request):
    """ 获取验证码 """
    mobile = request.POST.get('mobile', None)

    err, data = usecases.api_fetch_smscode(request, mobile)
    if err != ERROR_NO_ERROR:
        return ajax.error_response(err['message'], err['id'])

    return ajax.success_response(data, '验证码已发送!')

def api_fetch_token(request):
    """ 获取AccessToken """
    mobile = request.POST.get('mobile', None)
    sms_code = request.POST.get('sms_code', None)

    err, data = usecases.api_fetch_token(request, mobile, sms_code)
    if err != ERROR_NO_ERROR:
        return ajax.error_response(err['message'], err['id'])

    return ajax.success_response(data)

@mobile_token_required
def api_withdraw_user_info(request):
    """ 获取用户充值提现信息 """
    mobile = request.POST.get('mobile', None)

    err, data = usecases.api_withdraw_user_info(request, mobile)
    Log.info(f"api_withdraw_user_info: mobile={mobile}, data={data}")
    if err != ERROR_NO_ERROR:
        return ajax.error_response(err['message'], err['id'])

    return ajax.success_response(data)

@mobile_token_required
def api_withdraw_person_auth(request):
    """ 信息认证 """
    mobile = request.POST.get('mobile', None)
    sms_code = request.POST.get('sms_code', None)
    real_name = request.POST.get('real_name', None)
    cert_no = request.POST.get('cert_no', None)
    bank_name = request.POST.get('bank_name', None)
    bank_card_number = request.POST.get('bank_card_number', None)
    bank_address = request.POST.get('bank_address', None)
    bank_branch = request.POST.get('bank_branch', None)

    # 检测验证码是否合法
    if not sms_mgr.is_valid_sms_code(mobile, sms_code):
        return ajax.error_response(ERROR_INVALID_SMS_CODE['message'], ERROR_INVALID_SMS_CODE['id'])

    return _json(haibao_util.save_user_withdraw_auth_info(mobile, real_name, cert_no, bank_name, bank_card_number, bank_address, bank_branch))

    # err, data = usecases.api_withdraw_person_auth(request, mobile, sms_code, real_name, cert_no, bank_name, bank_card_number, bank_address, bank_branch)
    # if err != ERROR_NO_ERROR:
    #     return ajax.error_response(err['message'], err['id'])
    #
    # return ajax.success_response(data)

@mobile_token_required
def api_withdraw_fee(request):
    """ 计算提现费用 """
    mobile = request.POST.get('mobile', None)
    amount = int(request.POST.get('amount', 0))
    gem_type = int(request.POST.get('gem_type', 0))

    err, data = usecases.api_withdraw_fee(request, mobile, amount, gem_type)
    if err != ERROR_NO_ERROR:
        return ajax.error_response(err['message'], err['id'])

    return ajax.success_response(data)

@mobile_token_required
def api_withdraw_submit(request):
    """ 提现 """
    mobile = request.POST.get('mobile', None)
    amount = int(request.POST.get('amount', 0))
    gem_type = int(request.POST.get('gem_type', 0))
    return _json(haibao_util.user_submit_withdraw(mobile, amount, gem_type))

    # err, data = usecases.api_withdraw_submit(request, mobile, amount, gem_type)
    # if err != ERROR_NO_ERROR:
    #     return ajax.error_response(err['message'], err['id'])
    #
    # return ajax.success_response(data)

# -----------------------------------------------------------
# Part of 圣诞活动
# -----------------------------------------------------------

@uid_token_required
def api_christmas_info(request):
    """ 获取圣诞活动信息 """
    uid = request.POST.get('uid', None)
    return _json(haibao_util.get_christmas_activity_info(uid))

@uid_token_required
def api_christmas_open_box(request):
    """ 开启圣诞宝箱 """
    uid = request.POST.get('uid', None)
    box_type = int(request.POST.get('box_type', 0))
    count = int(request.POST.get('count', 1))
    return _json(haibao_util.open_christmas_box(uid, box_type, count))

@uid_token_required
def api_christmas_log(request):
    """ 获取圣诞礼品列表 """
    uid = request.POST.get('uid', None)
    count = int(request.POST.get('count', 1))
    offset = int(request.POST.get('offset', 0))
    return _json(haibao_util.get_christmas_reward_list(uid, count, offset))

# -----------------------------------------------------------
# Part of 新年活动
# -----------------------------------------------------------

@uid_token_required
def api_cny_sign_info(request):
    """ 获取新年签到活动信息 """
    uid = request.POST.get('uid', None)
    return _json(haibao_util.get_chinese_new_year_sign_activity_info(uid))

@uid_token_required
def api_cny_sign(request):
    """ 新年签到 """
    uid = request.POST.get('uid', None)
    day = int(request.POST.get('day', 0))
    return _json(haibao_util.chinese_new_year_sign(uid, day))

@uid_token_required
def api_cny_room_rankings(request):
    """ 烟火房间榜 """
    uid = request.POST.get('uid', None)
    return _json(haibao_util.fireworks_room_rankings(uid))

@uid_token_required
def api_cny_consume_rankings(request):
    """ 烟火个人榜 """
    uid = request.POST.get('uid', None)
    return _json(haibao_util.fireworks_consume_rankings(uid))

# -----------------------------------------------------------
# Part of 实名认证
# -----------------------------------------------------------

@uid_token_required
def api_user_auth_info(request):
    """ 查询用户审核信息 """
    uid = request.POST.get('uid', None)
    return _json(haibao_util.get_user_auth_info(uid))

@uid_token_required
def api_fetch_qiniu_upload_token(request):
    """ 获取上传图片的token """
    uid = request.POST.get('uid', None)
    file_type = request.POST.get('file_type', None)
    return _json(haibao_util.get_qiniu_upload_token(uid, file_type))

@uid_token_required
def api_upload_identity_info(request):
    """ 上传认证资料 """
    uid = request.POST.get('uid', None)
    sms_code = request.POST.get('sms_code', '')
    real_name = request.POST.get('real_name', '')
    id_number = request.POST.get('id_number', '')
    id_upside_image = request.POST.get('id_upside_image', '')
    id_downside_image = request.POST.get('id_downside_image', '')
    id_with_user_image = request.POST.get('id_with_user_image', '')

    # 检测验证码是否合法
    user = user_mgr.get_user_detail(uid)
    if user is None:
        return ajax.error_response(ERROR_INVALID_UID['message'], ERROR_INVALID_UID['id'])

    mobile = user['mobile']
    if not sms_mgr.is_valid_sms_code(mobile, sms_code):
        return ajax.error_response(ERROR_INVALID_SMS_CODE['message'], ERROR_INVALID_SMS_CODE['id'])

    return _json(haibao_util.upload_user_identity_info(uid, real_name, id_number, id_upside_image, id_downside_image, id_with_user_image))


@uid_token_required
def api_alipay_certify(request):
    """ 支付宝身份验证 """
    uid = request.POST.get('uid', None)
    sms_code = request.POST.get('sms_code', '')
    cert_name = request.POST.get('cert_name', '')
    cert_no = request.POST.get('cert_no', '')

    # 检测验证码是否合法
    user = user_mgr.get_user_detail(uid)
    if user is None:
        return ajax.error_response(ERROR_INVALID_UID['message'], ERROR_INVALID_UID['id'])

    mobile = user['mobile']
    if not sms_mgr.is_valid_sms_code(mobile, sms_code):
        return ajax.error_response(ERROR_INVALID_SMS_CODE['message'], ERROR_INVALID_SMS_CODE['id'])

    return _json(haibao_util.alipay_user_certify(uid, cert_name, cert_no))


@uid_token_required
def api_alipay_certify_query(request):
    """ 支付宝身份认证结果查询 """
    uid = request.POST.get('uid', None)
    return _json(haibao_util.alipay_user_certify_status(uid))


# -----------------------------------------------------------
# Part of 人气周星榜
# -----------------------------------------------------------

@uid_token_required
def api_weekly_star_info(request):
    """ 获取人气周星榜活动信息请求 """
    uid = request.POST.get('uid', None)
    return _json(haibao_util.get_weekly_gift_activity_info(uid))

@uid_token_required
def api_weekly_star_income_ranking(request):
    """ 周星活动的人气榜 """
    uid = request.POST.get('uid', None)
    count = int(request.POST.get('count', 0))
    offset = int(request.POST.get('offset', 0))
    return _json(haibao_util.get_weekly_gift_income_rankings(uid, count, offset))

@uid_token_required
def api_weekly_star_consume_ranking(request):
    """ 周星活动的贡献榜 """
    uid = request.POST.get('uid', None)
    count = int(request.POST.get('count', 0))
    offset = int(request.POST.get('offset', 0))
    return _json(haibao_util.get_weekly_gift_consume_rankings(uid, count, offset))

@uid_token_required
def api_follow_user(request):
    """ 关注用户 """
    uid = request.POST.get('uid', None)
    target_uid = int(request.POST.get('target_uid', None))
    return _json(haibao_util.follow_user(uid, target_uid))

# -----------------------------------------------------------
# Part of 幸运转盘活动
# -----------------------------------------------------------

@uid_token_required
def api_lucky_spin_info(request):
    """ 获取幸运转盘活动信息 """
    uid = request.POST.get('uid', None)
    return _json(haibao_util.get_lucky_spin_activity_info(uid))

@uid_token_required
def api_lucky_spin_logs(request):
    """ 获取幸运转盘活动信息 """
    uid = request.POST.get('uid', None)
    count = int(request.POST.get('count', 1))
    offset = int(request.POST.get('offset', 0))
    count = min(count, 50)
    return _json(haibao_util.get_lucky_spin_activity_log(uid, count, offset))

@uid_token_required
def api_lucky_spin_play(request):
    """ 获取幸运转盘活动信息 """
    uid = request.POST.get('uid', None)
    count = int(request.POST.get('count', 1))
    return _json(haibao_util.lucky_spin(uid, count))

# -----------------------------------------------------------
# Part of 首充活动
# -----------------------------------------------------------

@uid_token_required
def api_first_pay_info(request):
    """ 首充活动信息 """
    uid = request.POST.get('uid', None)
    return _json(haibao_util.get_first_payment_activity_info(uid))

@uid_token_required
def api_first_pay_collect(request):
    """ 首充活动领奖 """
    uid = request.POST.get('uid', None)
    amount = int(request.POST.get('amount', 0))
    return _json(haibao_util.take_first_payment_activity_reward(uid, amount))

# -----------------------------------------------------------
# Part of 累充活动
# -----------------------------------------------------------

@uid_token_required
def api_total_pay_info(request):
    """ 累充活动信息 """
    uid = request.POST.get('uid', None)
    return _json(haibao_util.get_total_payment_activity_info(uid))

@uid_token_required
def api_total_pay_collect(request):
    """ 累充活动领奖 """
    uid = request.POST.get('uid', None)
    the_type = int(request.POST.get('type', 1))
    amount = int(request.POST.get('amount', 0))
    return _json(haibao_util.take_total_payment_activity_reward(uid, the_type, amount))

# -----------------------------------------------------------
# Part of 五一活动
# -----------------------------------------------------------

@uid_token_required
def api_labor_day_room_ranking(request):
    """ 最强公会榜 """
    return _json(haibao_util.get51_room_income_rankings())

@uid_token_required
def api_labor_consume_ranking(request):
    """ 至尊富豪榜 """
    return _json(haibao_util.get51_consume_rankings())

if __name__ == '__main__':
    print('ok')
